# Compiler and linker
CROSS_COMPILE = aarch64-v01c01-linux-gnu-
CC = $(CROSS_COMPILE)gcc
CXX= $(CROSS_COMPILE)g++
AR = $(CROSS_COMPILE)ar

# Directories
SRC_DIRS :=
SRC_DIRS += ./
SRC_DIRS += /home/zyd/3516dv500/Hi3519DV500_SDK_V2.0.1.1/smp/a55_linux/source/mpp/sample/common
INCLUDE :=
INCLUDE += ./
INCLUDE += /opt/linux/x86-arm/aarch64-v01c01-linux-gnu-gcc/aarch64-linux-gnu/include/c++/10.3.0/bits
INCLUDE += /home/zyd/3516dv500/Hi3519DV500_SDK_V2.0.1.1/smp/a55_linux/source/mpp/sample/common
INCLUDE += /home/zyd/3516dv500/Hi3519DV500_SDK_V2.0.1.1/smp/a55_linux/source/out/include
INCLUDE += /home/zyd/3516dv500/Hi3519DV500_SDK_V2.0.1.1/smp/a55_linux/vendor/extdrv/lt8618sx
#INCLUDE += /home/cnop/toolof3519dv500/openssl-1.1.1w/__install/include
#INCLUDE += /mnt/hgfs/hi3516/newqt/projectsrc/include/global
#INCLUDE += /home/cnop/toolof3519dv500/curl-7.73.0/__install/include/curl

OBJ_DIR := obj
LIB_DIR := my_lib
APP_DIR := program

# Output
TARGET := test_rtsp_client

# Find all the source files
CXX_SOURCES := $(foreach dir, $(SRC_DIRS), $(wildcard $(dir)/*.cpp))
C_SOURCES := $(foreach dir, $(SRC_DIRS), $(wildcard $(dir)/*.c))

# Change source file paths to object paths
CXX_OBJECTS := $(patsubst %.cpp,$(OBJ_DIR)/%.o,$(CXX_SOURCES))
C_OBJECTS := $(patsubst %.c,$(OBJ_DIR)/%.o,$(C_SOURCES))

ALL_OBJECTS := $(CXX_OBJECTS) $(C_OBJECTS)

# Dependency files
DEPS := $(CXX_OBJECTS:.o=.d) $(C_OBJECTS:.o=.d)

SENSOR0_TYPE ?= SONY_IMX515_MIPI_8M_30FPS_12BIT
SENSOR1_TYPE ?= SONY_IMX415_2L_MIPI_8M_30FPS_12BIT
SENSOR2_TYPE ?= OV_OS04A10_MIPI_4M_30FPS_12BIT
SENSOR3_TYPE ?= OV_OS04A10_MIPI_4M_30FPS_12BIT

# Compiler flags
CXXFLAGS := -Wall -Wextra -O2 -pthread -fPIC -g -std=c++17 -mcpu=cortex-a55 -fno-aggressive-loop-optimizations -MMD -MP
CXXFLAGS += -DSENSOR0_TYPE=$(SENSOR0_TYPE)
CXXFLAGS += -DSENSOR1_TYPE=$(SENSOR1_TYPE)
CXXFLAGS += -DSENSOR2_TYPE=$(SENSOR2_TYPE)
CXXFLAGS += -DSENSOR3_TYPE=$(SENSOR3_TYPE)
CFLAGS := -Wall -Wextra -O2 -pthread -fPIC -g -std=c99 -mcpu=cortex-a55 -fno-aggressive-loop-optimizations -MMD -MP
CFLAGS += -DSENSOR0_TYPE=$(SENSOR0_TYPE)
CFLAGS += -DSENSOR1_TYPE=$(SENSOR1_TYPE)
CFLAGS += -DSENSOR2_TYPE=$(SENSOR2_TYPE)
CFLAGS += -DSENSOR3_TYPE=$(SENSOR3_TYPE)
# CFLAGS += -DOT_ACODEC_TYPE_INNER -DOT_VQE_USE_STATIC_MODULE_REGISTER
# CFLAGS += -Dhi3519dv500 -DOT_XXXX -DISP_V2 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
INCLUDE_DIR := $(addprefix -I,$(INCLUDE))

# Linker flags
LDFLAGS :=
LDFLAGS += -Llib
LDFLAGS += -L/home/zyd/3516dv500/Hi3519DV500_SDK_V2.0.1.1/smp/a55_linux/source/out/lib
#LDFLAGS += -L/home/cnop/toolof3519dv500/openssl-1.1.1w/__install/lib
#LDFLAGS += -L/home/cnop/toolof3519dv500/curl-7.73.0/__install/lib

LDLIBS :=
LDLIBS += -laac_comm -laac_dec -laac_enc -laac_sbr_dec -laac_sbr_enc -lacs -laiisp -lbnr -lcalcflicker -ldehaze -ldnvqe -ldrc -lextend_stats -lfileformat -lheif -lir_auto -lldci -lmp3_dec -lmp3_enc -lmp3_lame -lopus -lot_mpi_isp -lprotobuf-c -lsecurec -lsmmz_agent   -lsns_gst412c    -lsns_imx347_slave  -lsns_imx515       -lsns_os04a10 -lsns_os04a10_slave -lsns_os08a20 -lsns_sc450ai -lsns_sc850sl -lss_ivs_md -lss_mpi -lss_mpi_ae -lss_mpi_ai3dnr -lss_mpi_aibnr -lss_mpi_aidestrip -lss_mpi_aidrc -lss_mpi_audio -lss_mpi_audio_adp -lss_mpi_awb -lss_mpi_cipher -lss_mpi_dpu_match -lss_mpi_dpu_rect -lss_mpi_fisheye_calibrate -lss_mpi_isp -lss_mpi_ive -lss_mpi_km -lss_mpi_mcf -lss_mpi_mcf_calibrate -lss_mpi_motionfusion -lss_mpi_nuc -lss_mpi_otp -lss_mpi_snap -lss_mpi_sysbind -lss_mpi_sysmem -lss_mpi_thermo -lss_mpi_uvc -lss_stitch_lut -lss_tde -lsvp_acl -lsvp_aicpu -lupvqe -lvoice_engine -lvqe_aec -lvqe_agc -lvqe_anr -lvqe_common -lvqe_eq -lvqe_hpf -lvqe_record -lvqe_res -lvqe_talkv2
#LDLIBS += -lcrypto
#LDLIBS += -lcurl

# Rules
.PHONY: all clean cleanlib cleanall static shared add-lib-prefix help

all: $(TARGET)

$(TARGET): $(ALL_OBJECTS)
	$(CXX) $(LDFLAGS) -o $@ $(ALL_OBJECTS) $(LDLIBS)
	@cp $(TARGET) $(APP_DIR)/

# Static library creation for each object file you should first to make all to create .o files
static: $(patsubst $(OBJ_DIR)/%.o,$(LIB_DIR)/%.a,$(ALL_OBJECTS)) add-lib-prefix

$(LIB_DIR)/%.a: $(OBJ_DIR)/%.o
	@mkdir -p $(dir $@)
	$(AR) rcs $@ $<

# Shared library creation for each object file you should first to make all to create .o files
shared: $(patsubst $(OBJ_DIR)/%.o,$(LIB_DIR)/%.so,$(ALL_OBJECTS)) add-lib-prefix

$(LIB_DIR)/%.so: $(OBJ_DIR)/%.o
	@mkdir -p $(dir $@)
	$(CC) -shared -o $@ $<

# Pattern rules for building object files
$(OBJ_DIR)/%.o: %.cpp
	@mkdir -p $(@D)
	$(CXX) $(INCLUDE_DIR) $(CXXFLAGS) -c -o $@ $<

$(OBJ_DIR)/%.o: %.c
	@mkdir -p $(@D)
	$(CC) $(INCLUDE_DIR) $(CFLAGS) -c -o $@ $<

# Include the dependency files if they exist
-include $(DEPS)

clean:
	rm -rf $(TARGET) $(OBJ_DIR)

cleanlib:
	rm -rf $(LIB_DIR)

cleanall:
	rm -rf $(OBJ_DIR) $(TARGET) $(LIB_DIR)

add-lib-prefix:
	@find $(LIB_DIR) -type f | while read FILE; do \
		DIR=$$(dirname "$$FILE"); \
		BASENAME=$$(basename "$$FILE"); \
		if [ "$${BASENAME#lib}" = "$$BASENAME" ]; then \
			mv "$$FILE" "$$DIR/lib$$BASENAME"; \
		fi; \
	done

# Help information
help:
	@echo "可用命令："
	@echo "  all              - 编译并链接所有源文件，生成目标可执行文件 $(TARGET)"
	@echo "  static           - 创建静态库，需先执行 all 来生成 .o 文件"
	@echo "  shared           - 创建共享库，需先执行 all 来生成 .o 文件"
	@echo "  clean            - 删除目标可执行文件 $(TARGET)"
	@echo "  cleanlib         - 删除生成的库文件"
	@echo "  cleanall         - 删除所有生成的目标文件、可执行文件和库"
	@echo "  add-lib-prefix   - 为库文件添加 'lib' 前缀"
	@echo "  help             - 显示此帮助信息"
